package com.pearadmin.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearadmin.common.nucleus.base.BaseController;
import com.pearadmin.common.nucleus.domain.request.PageParam;
import com.pearadmin.common.nucleus.domain.response.Result;
import com.pearadmin.common.nucleus.domain.response.TableVO;
import com.pearadmin.system.domain.SysDictType;
import com.pearadmin.system.service.ISysDictTypeService;
import org.activiti.engine.management.TablePage;
import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Arrays;


@RestController
@RequestMapping("system/dictType")
public class SysDictTypeController extends BaseController {

    private String MODULE_PATH = "system/dictType/";

    @Resource
    private ISysDictTypeService sysDictTypeService;

    @GetMapping("main")
    public ModelAndView main(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH+"main");
        return modelAndView;
    }

    @GetMapping("data")
    public TableVO data(SysDictType sysDictType, PageParam pageParam){
       IPage<SysDictType> pageInfo = sysDictTypeService.page(new Page<>(pageParam.getPage(),pageParam.getLimit()),null);
       return PageTable(pageInfo.getRecords(),pageInfo.getTotal());
    }

    @GetMapping("add")
    public ModelAndView add(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH+"add");
        return modelAndView;
    }

    @PostMapping("save")
    public Result save(@RequestBody SysDictType sysDictType){
        boolean result = sysDictTypeService.save(sysDictType);
        return choice(result);
    }

    @GetMapping("edit")
    public ModelAndView edit(ModelAndView modelAndView,String id){
        SysDictType sysDictType =  sysDictTypeService.getById(id);
        modelAndView.addObject("sysDictType",sysDictType);
        modelAndView.setViewName(MODULE_PATH+"edit");
        return modelAndView;
    }

    @PutMapping("update")
    public Result update(@RequestBody  SysDictType sysDictType){
        boolean result = sysDictTypeService.updateById(sysDictType);
        return choice(result);
    }

    @PutMapping("enable/{id}")
    public Result  enable(@PathVariable String id){
        SysDictType sysDictType = new SysDictType();
        sysDictType.setStatus("0");
        sysDictType.setId(id);

        boolean result = sysDictTypeService.updateById(sysDictType);
        return choice(result);
    }

    @PutMapping("disable/{id}")
    public Result disable(@PathVariable String id){
        SysDictType sysDictType = new SysDictType();
        sysDictType.setStatus("1");
        sysDictType.setId(id);

        boolean result = sysDictTypeService.updateById(sysDictType);
        return  choice(result);
    }

    @DeleteMapping("deleteById/{id}")
    public  Result deleteById(@PathVariable String id){
        boolean result = sysDictTypeService.removeById(id);
        return choice(result);
    }

    @DeleteMapping("deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable String ids){

        boolean result = sysDictTypeService.removeByIds(Arrays.asList(ids.split(",")));
        return choice(result);
    }
}
